package org.wenbo.override.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Session {
	private HttpSession session;

	private Session(HttpSession session) {
		this.session = session;
	}

	public static Session me(HttpServletRequest request) {
		return new Session(request.getSession());
	}

	public static Session me(HttpSession session) {
		return new Session(session);

	}

	private String getClassName(Object o){
		String name=o.getClass().getName().substring(o.getClass().getName().lastIndexOf("."));
		name=name.replace(".A","a");
		name=name.replace(".B","b");
		name=name.replace(".C","c");
		name=name.replace(".D","d");
		name=name.replace(".E","e");
		name=name.replace(".F","f");
		name=name.replace(".G","g");
		name=name.replace(".H","h");
		name=name.replace(".I","i");
		name=name.replace(".J","j");
		name=name.replace(".K","k");
		name=name.replace(".L","l");
		name=name.replace(".M","m");
		name=name.replace(".N","n");
		name=name.replace(".O","o");
		name=name.replace(".P","p");
		name=name.replace(".Q","q");
		name=name.replace(".R","r");
		name=name.replace(".S","s");
		name=name.replace(".T","t");
		name=name.replace(".U","u");
		name=name.replace(".V","v");
		name=name.replace(".W","w");
		name=name.replace(".X","x");
		name=name.replace(".Y","y");
		name=name.replace(".Z","z");
		
		System.out.println("-----");
		System.out.println(name);
		System.out.println("-----");
		return name;
	}
	
	@SuppressWarnings("unchecked")
	public <C> C getObject(Class<C> type) {
		return (C) session.getAttribute(type.getName());
	}

	public void setObject(Object o) {
		session.setAttribute(getClassName(o), o);
	}
	
	
	public boolean hasObj(Object o){
		return null != session.getAttribute(getClassName(o));
	}
	public boolean hasObj(String name){
		return null != session.getAttribute(name);
	}
	public void set(String name,Object o){
		session.setAttribute(name, o);
	}
	public void set(Object o){
		session.setAttribute(getClassName(o), o);
	}
	public void remove(String name){
		session.removeAttribute(name);
	}
	public void remove(Object o){
		session.removeAttribute(getClassName(o));
	}
	public Class<?> get(String name){
		return (Class<?>) session.getAttribute(name);
	}
}
